package edu.tcc.iot.persistence;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ResourceBundle;

import javax.sql.DataSource;

public class DBContext {

	private DataSource dataSource;
	private ResourceBundle productInfoQueryBundle;
	private ResourceBundle productQueryBundle;
	private ResourceBundle shelfQueryBundle;

	public DBContext(){
		initiate();
	}

	private void initiate() {
		try{
			productInfoQueryBundle = ResourceBundle.getBundle("product_info_querys");
			productQueryBundle = ResourceBundle.getBundle("product_querys");
			shelfQueryBundle = ResourceBundle.getBundle("shelf_querys");
			dataSource = DaoConnectionFactory.createDataSource();
		}catch (Throwable ex) { 
			System.err.println("Failed to create dataSource object." + ex);
			throw new ExceptionInInitializerError(ex); 
		}
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public String getProductQuery(String path, String... args) {
		return productQueryBundle.getString(path);
	}

	public String getShelfQuery(String path, String... args) {
		return shelfQueryBundle.getString(path);
	}

	public String getProductInfoQuery(String path, String... args) {
		return productInfoQueryBundle.getString(path);
	}
	
	public Connection getConnection() throws SQLException{
		Connection conn =  dataSource.getConnection();
		conn.setAutoCommit(true);
		return conn;
	}
}
